/*
 * hw_board.h
 *
 *  Created on: 2025 Jan 24
 *      Author: SFLY
 */

#ifndef MOTOR_SOURCE_HW_DRIVERS_HW_BOARD_H_
#define MOTOR_SOURCE_HW_DRIVERS_HW_BOARD_H_
#include "driverlib.h"
#include "device.h"
#include "bsp_adc.h"
#include "bsp_pwm.h"
#include "bsp_can.h"
#include "bsp_timer.h"
#include "bsp_flash.h"
#include "bsp_uart.h"
#include "bsp_encoder_as5047p.h"
/********************电机基础参数***********************************/
#define MOTOR_PolePairs                     (21)  //极对数
#define MOTOR_ReductionRatio                (9)   //减速比
#define Ke                                  (2.1) //N.m
#define V_BASE                              (60)  //V
#define I_BASE                              (90)  //A
#define F_BASE                              (100) //Hz
#define INV_V_BASE                           __builtin_dsp_fsdiv_qx320f(1.0f, V_BASE)
#define INV_I_BASE                           __builtin_dsp_fsdiv_qx320f(1.0f, I_BASE)
#define INV_F_BASE                           __builtin_dsp_fsdiv_qx320f(1.0f, F_BASE)
/*********************************************************************/

/********************硬件板级基本信息************/
#define MOTOR_SYS_PWM_SWITCHING_FREQ_HZ     (16000)
#define MOTOR_SYS_PWM_DEAD_TIME_NS          (1000)
#define Motor_SYS_TICK_FREQ_HZ              (1000)
#define DEVICE_SYSCLK_FREQ_MHZ              (DEVICE_SYSCLK_FREQ / 1000000U)
#define INV_DEVICE_SYSCLK_FREQ              (1.0/DEVICE_SYSCLK_FREQ)
#define USER_NUM_PWM_TICKS_PER_ISR_TICK         (1)
//ADC采样参数
#define ADC_VOLT_REF            (3.3f)      /** reference voltage of ADC        */
#define ADC_BIT_LENGTH          12          /** 12-bit ADC                      */
#define ADC_MAX_RANGE           (4095)      /** range of ADC result  */
#define Current_AmplifierRs     (0.001f)
#define Current_AmplifierGain   (40.3f)
#define Current_OffsetCheckTs   (50.0f)
#define Vbus_Gain               (31.0f)
#define CurrentSampleFs         (15000)
//编码器基本参数
#define ENCODER_STRUCT               1      //单编码器模式:0 双编码器差速模式（编码器都在同一侧）:1 双编码器模式（输入轴、输出轴各有一个编码器）:2
#define Motor_End_Encoder_Res     (16384)//电机端磁编码器分辨率(0-Res)<==>(0,360)<==>(0,2PI)
#define Reducer_End_Encoder_Res   (16384)//减速器之后端磁编码器分辨率(0-Res)<==>(0,360)<==>(0,2PI),如果只有一个编码器，则和Motor_End_Encoder_Res相同
#define Motor_Encoder_Dir           (1)//编码器正方向和电机出轴方向顺时针旋转方向是否相同，如果相同是1，不相同是-1
#define Elec_ZeroOffset_TCount     (200)//电角度零位用计数
//编码器校准基本参数
#define LUT_N   128
#define CALIB_NT 40
#define CALIB_N LUT_N*MOTOR_PolePairs
#define CALIB_D_Cur  0.04f
#define CALIB_WINDOW  128
//PWM基本参数
#define PWM_MaxDutyRatio  (0.9f) //PWM模块输出最大值比例,防止PWM满功率输出后驱动电路异常
//CAN通信基本参数
#define MOTOR_CAN_ID              (0x001) //默认电机CAN ID
#define MASTER_CAN_ID             (0xfff)//默认主机 CAN ID
#define MOTOR_CAN_ID_MASK         (0xfff) //必须完全匹配该电机的CAN ID，才能进行数据处理
//保护基本参数
#define ABNORMAL_HIGH_VBUS        (40.0f)//V
#define ABNORMAL_LOW_VBUS         (6.0f)//V
#define OVP_VBUS_THRESHOLD        (40.0f)//V
#define UVP_VBUS_THRESHOLD        (6.0f)//V
#define OVP_VBUS_TIMEUS           (500)//us
#define UVP_VBUS_TIMEUS           (500)//us
#define ABNORMAL_TIMEUS           (500)//us
#define OVP_CURRENT_THRESHOLD     (60.0f)//A
#define OVP_CURRENT_TIMEUS        (100)//us
#define OVP_TEMP_THRESHOLD        (90.0f)//摄氏度
#define OVP_TEMP_TIMEUS           (500)//us
#define ERR_CLEAR_TIMEUS          (5000)//us

/**电机错误状态表***************************************/
#define ERR_NONE        0x0000U          /** no error */
#define ERR_AD_OFFSET   0x0001U          /** AD offset error */
#define ERR_OC_PEAK     0x0002U          /** over-current: peak current */
#define ERR_OC_CMP      0x0004U          /** over-current: CMP current */
#define ERR_OC_HW       0x0008U          /** over-current: hardware over current */
#define ERR_VDC_OV      0x0010U          /** DC bus over voltage */
#define ERR_VDC_UV      0x0020U          /** DC bus under voltage */
#define ERR_VDC_ABNORM  0x0040U          /** abnormal DC bus voltage */
#define ERR_MOTOR_OP    0x0080U          /** motor over power */
#define ERR_MOTOR_OT    0x0100U          /** motor over temperature */
#define ERR_IPM_OT      0x0200U          /** IPM over temperature */
#define ERR_LOCK_ROTOR  0x0400U          /** motor rotor locked */
#define ERR_LACK_PHASE  0x0800U          /** motor lack phase */
#define ERR_COMM        0x1000U          /** communication error */
#define ERR_SWWD_INT    0x2000U          /** software watch-dog interrupt */
#define ERR_HWWD_INT    0x4000U          /** hardware watch-dog interrupt */
#define ERR_UNDEF_INT   0x8000U          /** undefined interrupt */
#define ERR_DMA_FAIL    0x00010000U      /** DMA transfer failed */
#define ERR_SYS_CLK     0x00020000U      /** system clock error */
#define ERR_IPD_ERR     0x00040000U      /** initial-position-detection error */
/*********************************************************************/

/********************驱动库基本信息***********************************/
#define VERSION_NUM "1.0"
//电机的五种状态
#define REST_MODE                  0
#define CALIBRATION_MODE           1
#define MOTOR_MODE                 2
#define SETUP_MODE                 4
#define ENCODER_MODE               5
/*********************************************************************/

/********************FOC算法基本参数************/
//Current PID Default Config
#define CUR_OUT_MAX        (80.0f)  //A
#define CUR_OUT_MIN        (-80.0f) //A
#define CUR_ERR_MAX        (40.0f)  //A
#define CUR_DEFAULT_KP     (4.0)//0.0004 记得需要从flash里读取后除以10000
#define CUR_DEFAULT_KI     (10000)//1 记得需要从flash里读取后除以10000
//Speed PID Default Config
#define SPD_OUT_MAX        (1200)  //rpm
#define SPD_OUT_MIN        (-1200) //rpm
#define SPD_ERR_MAX        (100)
#define SPD_DEFAULT_KP     (90)    // 记得需要从flash里读取后除以10000
#define SPD_DEFAULT_KI     (80)//0.02 记得需要从flash里读取后除以100000
#define SPD_LPF_FC         (500)//低通滤波器截至频率
#define SPD_LPF_FS         (1000)//低通滤波器采样频率

//Pose PID Default Config
#define POS_DEFAULT_KP     (1300)// 记得需要从flash里读取后除以10000
#define POS_DEFAULT_KI     (6600)// 记得需要从flash里读取后除以10000
#define POS_DEFAULT_KD     (350000)// 记得需要从flash里读取后除以10000


//Pose-Torque PID Default Config
#define POS_TOR_DEFAULT_KP (0.0f)//0 记得需要从flash里读取后除以10000
#define POS_TOR_DEFAULT_KD (10.0f)//0 记得需要从flash里读取后除以10000
/*********************************************************************/

void Bsp_Board_Init(void);
#endif /* MOTOR_SOURCE_HW_DRIVERS_HW_BOARD_H_ */
